import requests
from bs4 import BeautifulSoup


def get_kugou_hot_music():
    url = 'https://www.kugou.com/yy/html/rank.html'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
    try:
        response = requests.get(url, headers=headers)
        response.raise_for_status()
        response.encoding = response.apparent_encoding
        soup = BeautifulSoup(response.text, 'html.parser')
        # 更新选择器，根据实际情况调整
        print(music_list)
        music_list = soup.find_all('li', class_='')  # 需要根据实际情况更新类名

        for music in music_list:
            song_name_tag = music.find('a', class_='')  # 需要根据实际情况更新类名
            singer_tag = music.find('span', class_='')  # 需要根据实际情况更新类名
            if song_name_tag and singer_tag:
                song_name = song_name_tag.text.strip()
                singer = singer_tag.text.strip()
                play_url = song_name_tag.get('href')
                if play_url:
                    print(f"歌曲名称: {song_name}")
                    print(f"歌手: {singer}")
                    print(f"播放链接: {play_url}")
                    print("-" * 50)
    except requests.RequestException as e:
        print(f"请求出错: {e}")
    except Exception as e:
        print(f"发生未知错误: {e}")


if __name__ == "__main__":
    get_kugou_hot_music()
